﻿
namespace LS.Nc6.Application.System
{
    /// <summary>
    /// 提供菜单管理服务
    /// </summary>

    public class SysMenuService : BaseService<SysMenuEntity>, ISysMenuService, ITransient
    {
       
        public async Task<List<SysMenuFunctionEntity>> GetMenuFunction(Expression<Func<SysMenuFunctionEntity, bool>> expression)
        {
            return await DbContext.Instance.Queryable<SysMenuFunctionEntity>().Where(expression).ToListAsync(); 
        }

        public async Task<int> InsertMenuFun(SysMenuEntity model, List<SysMenuFunctionEntity> listModel)
        {
            try
            {
                DbContext.Instance.BeginTran();
                await DbContext.Instance.Insertable(model).ExecuteCommandAsync();
                if (listModel != null && listModel.Count > 0)
                {
                    await DbContext.Instance.Insertable(listModel).ExecuteCommandAsync();
                }
                DbContext.Instance.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                DbContext.Instance.RollbackTran();
                throw;
            }
        }

        public async Task<int> UpdateMenuFun(SysMenuEntity model, List<SysMenuFunctionEntity> listModel)
        {
            try
            {
                DbContext.Instance.BeginTran();
                await DbContext.Instance.Updateable(model).ExecuteCommandAsync();

                await DbContext.Instance.Deleteable<SysMenuFunctionEntity>().Where(a => a.MenuCode == model.MenuCode).ExecuteCommandAsync();

                if (listModel != null && listModel.Count > 0)
                {
                    await DbContext.Instance.Insertable(listModel).ExecuteCommandAsync();
                }
                DbContext.Instance.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                DbContext.Instance.RollbackTran();
                throw;
            }
        }

        public async Task<int> GetMaxPartMenu(long parentId)
        {
          return  await DbContext.Instance.Queryable<SysMenuEntity>().WhereIF(parentId == 0, it => it.Menulevel == 0)
                 .WhereIF(parentId > 0, it => it.ParentId == parentId).MaxAsync(it=>it.MenuSeq) ;
        }

        public List<SysMenuEntity> GetUserPartMenu(long userId, int mlevel)
        {
            if (999999 == userId)//管理员
            {
                //&& SqlFunc.Subqueryable<SysUserRoleEntity>().Where(sur => sur.UserId == userCode && sur.RoleCode == som.RoleCode
                return DbContext.Instance.Queryable<SysMenuEntity>().InnerJoin<SysRoleMenuEntity>((sm, som) => sm.MenuCode == som.MenuCode)
                    .Where((sm, som) => sm.Isenable == true && sm.Isenable == true )
                    .OrderBy((sm, som) => sm.MenuSeq)
                    .Select((sm,som) => new SysMenuEntity
                    {
                        Id = sm.Id,
                        MenuName = sm.MenuName,
                        MenuCode = sm.MenuCode,
                        MenuIcon = sm.MenuIcon,
                        MenuSeq = sm.MenuSeq,
                        Menulevel = sm.Menulevel,
                        TargetType =sm.TargetType,
                        ParentId = sm.ParentId,
                        Isvisible = sm.Isvisible,
                        MenuUrl = sm.MenuUrl, Rolecode =som.RoleCode, Funcode =som.FunCode})
                    .ToList();
                
            }
            else
            {
              return   DbContext.Instance.Queryable<SysMenuEntity>().InnerJoin<SysRoleMenuEntity>((sm, som) => sm.MenuCode == som.MenuCode)
                    .Where((sm, som) => sm.Isenable == true && sm.Isenable == true && SqlFunc.Subqueryable<SysUserRoleEntity>().Where(sur => sur.UserId == userId && sur.RoleCode == som.RoleCode).Any())
                    .OrderBy((sm, som) => sm.MenuSeq)
                    .Select((sm, som) => new SysMenuEntity { 
                        Id = sm.Id,
                        MenuName =sm.MenuName,
                        MenuCode = sm.MenuCode,
                        MenuIcon =sm.MenuIcon,
                        MenuSeq =sm.MenuSeq,
                        Menulevel= sm.Menulevel,
                        MenuUrl = sm.MenuUrl,
                        TargetType = sm.TargetType,
                        ParentId = sm.ParentId,
                        Isvisible = sm.Isvisible,
                        Rolecode = som.RoleCode, Funcode = som.FunCode })
                    .ToList();
            }
          
        }
       
       
       public async Task<List<SysMenuEntity>> MenuList(Expression<Func<SysMenuEntity, bool>> expression)
        {
            var menuFunctionList = await DbContext.Instance.Queryable<SysMenuFunctionEntity>().ToListAsync();
            var menuList= await DbContext.Instance.Queryable<SysMenuEntity>().Where(expression).OrderBy(a => a.MenuSeq).ToListAsync();
            foreach (var item in menuList)
            {
                item.Funcode =string.Join(",", menuFunctionList.Where(a=>a.MenuCode==item.MenuCode).Select(a=>a.FunCode).ToList());
            }
            return menuList;
            //return await DbContext.Instance.Queryable<SysMenuEntity>().Where(expression).Includes(a => a.MenuFunctionInfo).OrderBy(a => a.MenuSeq).ToListAsync();
        }
        public  async Task<bool> ExistsMenuCode(string menuCode)
        {
            return  await DbContext.Instance.Queryable<SysMenuEntity>().Where(it => it.MenuCode == menuCode).CountAsync()>0;
        }

    }
}
